namespace com.oakwoodsys.ConsultantCare.Features.WebLevel
{
    using System;
    using System.Runtime.InteropServices;

    using com.oakwoodsys.Exceptions.SharePointExceptions;
    using com.oakwoodsys.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid( "834463c9-f066-42d7-9031-11843431871f" )]
    public class WebLevelEventReceiver : SPFeatureReceiver
    {
        #region Fields

        static string _Title;

        #endregion Fields

        #region Properties

        public static Guid ID
        {
            get
            {
                return new Guid( "7e4f999d-7641-416b-87dc-d03edc025fc5" ); // from feature manifest
            }
        }

        public static string Title
        {
            get
            {
                if ( string.IsNullOrEmpty( _Title ) )
                    _Title = SPFarm.Local.GetFeatureDefinition( ID ).GetTitle( );
                return _Title;
            }
        }

        #endregion Properties

        #region Methods

        // Uncomment the method below to handle the event raised after a feature has been activated.
        public override void FeatureActivated( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPWeb ) )
                {
                    throw new FeatureActivationFailedNotScopedForWebException( ID, Title, properties.GetWebUrl( ) );
                }

                using ( Deletion_Definition_WebLevel o = new Deletion_Definition_WebLevel( properties ) )
                    o.Create( );

                using ( ConsultantCareConfiguration_Definition_WebLevel o = new ConsultantCareConfiguration_Definition_WebLevel( properties ) )
                    o.Create( );

                using ( Consultant_Definition_WebLevel o = new Consultant_Definition_WebLevel( properties ) )
                    o.Create( );

                using ( ConsultantCareActivity_Definition_WebLevel o = new ConsultantCareActivity_Definition_WebLevel( properties ) )
                    o.Create( );

                using ( ConsultantActivityID_Definition_WebLevel o = new ConsultantActivityID_Definition_WebLevel( properties ) )
                    o.Create( );

                using ( Recruiter_Definition_WebLevel o = new Recruiter_Definition_WebLevel( properties ) )
                    o.Create( );
            }
            catch ( Exception e )
            {
                throw new FeatureActivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.
        public override void FeatureDeactivating( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPWeb ) )
                {
                    throw new FeatureDeactivationFailedNotScopedForWebException( ID, Title, properties.GetWebUrl( ) );
                }

                using ( ConsultantCareConfiguration_Definition_WebLevel o = new ConsultantCareConfiguration_Definition_WebLevel( properties ) )
                    o.Delete( );

                using ( Consultant_Definition_WebLevel o = new Consultant_Definition_WebLevel( properties ) )
                    o.Delete( );

                using ( ConsultantCareActivity_Definition_WebLevel o = new ConsultantCareActivity_Definition_WebLevel( properties ) )
                    o.Delete( );

                using ( ConsultantActivityID_Definition_WebLevel o = new ConsultantActivityID_Definition_WebLevel( properties ) )
                    o.Delete( );

                using ( Recruiter_Definition_WebLevel o = new Recruiter_Definition_WebLevel( properties ) )
                    o.Delete( );

                using ( Deletion_Definition_WebLevel o = new Deletion_Definition_WebLevel( properties ) )
                    o.Delete( );
            }
            catch ( Exception e )
            {
                throw new FeatureDeactivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        #endregion Methods

        #region Other

        // Uncomment the method below to handle the event raised after a feature has been installed.
        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised before a feature is uninstalled.
        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised when a feature is upgrading.
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

        #endregion Other
    }
}